package com.wy.business.market.home.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.services.AbstractBaseService;

/**
 * 项目名称：openportal 类名称：SearchService 类描述：搜索 创建人：田松林 创建时间：2016-1-8 下午1:41:05
 * 修改人：田松林 修改时间：2016-1-8 下午1:41:05 版本信息：@version 1.0 Copyright (c)
 * 2016万雍科技（上海）有限公司-版权所有 修改备注：
 * 
 * @version
 */
@Service
public class ContentService extends AbstractBaseService {

	@SuppressWarnings({ "rawtypes" })
	public List<Map> querySearchResult(String userId, String searchContent)
			throws ErrorSqlException {
		List<Map> queryResult = new ArrayList<Map>();

//		queryResult.add(queryNews(searchContent, searchType));
//		queryResult.add(queryApps(userId, searchContent, searchType));
//		queryResult.add(queryContacts(userId, searchContent, searchType));
//		queryResult.add(queryCharts(userId, searchContent, searchType));
		
		//资讯
		Map<String,Object> newsMap=queryNews(searchContent);
		if (Integer.parseInt(newsMap.get("total").toString())>0) {
			queryResult.add(newsMap);
		}
		//发现
		Map<String,Object> AppsMap=queryApps(userId, searchContent);
		if (Integer.parseInt(AppsMap.get("total").toString())>0) {
			queryResult.add(AppsMap);
		}
		//联系人,userId传空,只给资讯和发现
		if(StringUtils.isNotEmpty(userId)){
			Map<String,Object> contactMap=queryContacts(userId, searchContent);
			if (Integer.parseInt(contactMap.get("total").toString())>0) {
				queryResult.add(contactMap);
			}
		}
		return queryResult;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> queryNews(String searchContent)
			throws ErrorSqlException {
		List<Map> list = dataBaseDao.queryForList("t_market_news.search", "%"+ searchContent + "%");

		return getResult(list, "资讯", null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> queryApps(String userId, String searchContent) throws ErrorSqlException {
		List<Map> list;
		if(StringUtils.isNotEmpty(userId)){
			List<Object> params = new ArrayList<Object>();
			params.add(userId);
			params.add("%" + searchContent + "%");
			list = dataBaseDao.queryForList("t_sys_function.search", params);
		}else{
			List<Object> params = new ArrayList<Object>();
			params.add("%" + searchContent + "%");
			list = dataBaseDao.queryForList("t_sys_function.searchNotUserId", params);
		}
		return getResult(list, "应用", null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> queryContacts(String userId,String searchContent) throws ErrorSqlException {
		List<Object> params = new ArrayList<Object>();
		params.add(userId);
		params.add("%" + searchContent + "%");
		List<Map> list = dataBaseDao.queryForList("T_Market_imcontact.search",
				params);

		return getResult(list, "联系人", null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> queryCharts(String userId, String searchContent) throws ErrorSqlException {
		List<Object> params = new ArrayList<Object>();
		params.add(userId);
		params.add("%" + searchContent + "%");
		List<Map> list = dataBaseDao.queryForList("T_Market_imchats.search",
				params);

		return getResult(list, "聊天记录", searchContent);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> getResult(List<Map> list, String type,String name) {
		Map resuMap = new HashMap<String, Object>();
		int total = 0;
		if (StringUtils.equals(type, "聊天记录")) {
			for (Map map : list) {
				total += Integer.parseInt(map.get("chartCount").toString());
				map.put("chartCount", map.get("chartCount") + "条关于" + name
						+ "的聊天");
			}
		} else {
			total = list.size();
		}

		resuMap.put("detail", list);

		resuMap.put("total", list.size());
		resuMap.put("name", type + "(" + total + ")");

		return resuMap;

	}

	@SuppressWarnings("rawtypes")
	public List<Map> getTop(List<Map> list, int n) {
		List<Map> newList = new ArrayList<Map>();
		for (int i = 0; i < n; i++) {
			newList.add(list.get(i));
		}
		return newList;
	}

}
